package y1s.controller;

import java.awt.Cursor;
import java.awt.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import y1s.data.Data;
import y1s.data.Memcache;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

@SuppressWarnings("serial")
public class InsertServlet extends HttpServlet {
	public static void main(String[] args) {
		
		DBCollection coll = Data.data("domain");
//		DBCursor domain = coll1.find(new BasicDBObject("status",0)).limit(1);
//		if(domain.size() > 0)
//		{
//			System.out.println(domain.next().get("name").toString());
//		}
//		DBCollection coll = Data.data("domain");

//		Memcache.setMemcache("last-updates", "abc");
//		
//		String[] arrDomain = Memcache.getMemcache("last-updates");
//		if(arrDomain != null)
//		{
//			for (int i = 0; i < arrDomain.length; i++) {
//				System.out.println(arrDomain[i]);
//			}
//		}
		
//		DBCursor db = coll.find(new BasicDBObject("lKeyword",new BasicDBObject("$exists",false)));
////		System.out.println(db);
//		while(db.hasNext())
//		{
//			DBObject dbObject = db.next();
////			System.out.println(dbObject);
//			ArrayList<String> arrayList = new ArrayList<String>();
//			if(dbObject.get("keyword")!= null)
//			{
//				arrayList = Utils.returnArrayList(dbObject.get("keyword").toString());
//			}
//			ArrayList<String> arrayList1 = new ArrayList<String>();
//			if(dbObject.get("pKeyword")!= null)
//			{
//				arrayList1 = Utils.returnArrayList(dbObject.get("pKeyword").toString());
//			}
////			System.out.println(arrayList.size());
//
//			dbObject.put("lKeyword", arrayList);
//			dbObject.put("lpKeyword", arrayList1);
////			coll.save(dbObject2,coll.getWriteConcern());
////			System.out.println(dbObject);
//			if(dbObject.get("name")!=null && 
//					!dbObject.get("name").toString().equals("cunit.sourceforge.net")
//					&&!dbObject.get("name").toString().equals("hameddanger.deviantart.com")
//					&&!dbObject.get("name").toString().equals("cracked.com")
//					&&!dbObject.get("name").toString().equals("av99bon.8d.com.tw")
//					&&!dbObject.get("name").toString().equals("remex-zw.de")
//					&&!dbObject.get("name").toString().equals("wosoft.ru")
//					&&!dbObject.get("name").toString().equals("eda.ucoz.ru")
//					&&!dbObject.get("name").toString().equals("store.123greetings.com+123greetings.com")
//					&&!dbObject.get("name").toString().equals("wellyee.com")
//					&&!dbObject.get("name").toString().equals("cuvoice.com")
//					&&!dbObject.get("name").toString().equals("whqy365.com")
//					&&!dbObject.get("name").toString().equals("zjrarr.b2b.hc360.com")
//					&&!dbObject.get("name").toString().equals("njx168.com")
//			)
//				try {
//					coll.findAndModify(new BasicDBObject("name",dbObject.get("name").toString()), dbObject);
//				} catch (Exception e) {
//					// TODO: handle exception
//				}
////			coll.
////			coll.remove(db);
//		}
		DBObject object = coll.findOne(new BasicDBObject("name","85st.av99.us"));
		object.put("possex", "1");
		coll.findAndModify(new BasicDBObject("name","85st.av99.us"), object);
//		System.out.println(db);
//		coll.createIndex(new BasicDBObject("update", -1));
//		System.out.println(coll.getIndexInfo());
//		DBCursor db = coll.find();
//		DBCollection colls = Data.data("link");
//		while(db.hasNext()){
////			System.out.println(db.next().get("link"));
//			DBObject dd = db.next(); 
//			if(dd.get("link") != null)
//			{
//				String str = returnString(dd.get("link").toString());
//				if(str != "")
//				{
//					String[] sbs = str.split("\\|");
////					System.out.println(dd.get("name")+"|");
//					for (int i = 0; i < sbs.length; i++) {
//						DBObject dbCheck = colls.findOne(new BasicDBObject("name",dd.get("name").toString()).append("link", sbs[i].toLowerCase().trim()));
//		        		if(dbCheck == null)
//		        		{
//		        			BasicDBObject inLink = new BasicDBObject();
//			        		inLink.put("name", dd.get("name"));
//			        		inLink.put("link", sbs[i].toLowerCase().trim());
//			        		colls.insert(inLink);
////		        			System.out.println(sbs[i]);
//		        		}
//					}
//				}
//			}
//		}
	}
	
	public static String returnString(String content) {
		String[] sbc = null;
//		System.out.println(content);
		String str = "";
		if(content != null)
		{
		sbc= content.split(": 1");
		
		for (int i = 0; i < sbc.length; i++) {
			if (!sbc[i].replaceAll("[,{}\\\"]+", "").trim().equals("")) {
				
				str += sbc[i].replaceAll("[,{}\\\"]+", "").trim().replaceAll("[&]{3}", ".")+"|";
				
			}
		}
		}
		return str;
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello, world");
		
		
		
//		String name = req.getParameter("name").toLowerCase().replaceAll("(http|https)+://(www.)*", "").replaceAll("^[\\w\\W]+[\\/]{2}", "").replaceAll("[\\/]+[\\w\\W]+$", "").replaceAll("www.", "");
		
		DBCollection coll = Data.data("domain");
//		System.out.println(coll.getIndexInfo());

//		int domain = coll.find().count();
//		System.out.println(domain);
//		while(domain.hasNext())
//		{
//			System.out.println(domain.next().get("name"));
//		}
//		
//		coll.createIndex(new BasicDBObject("link", -1));
		
//		DBObject check = coll.findOne(new BasicDBObject("name",name));
//		
//		if(check == null)
//		{
//			final Process process = Runtime.getRuntime().exec("whois "+name);
//			BufferedReader buf = new BufferedReader(new InputStreamReader(
//			process.getInputStream()));
//			String line;
//			String whois="";
//			while ((line = buf.readLine()) != null) {
//				whois += line+"<br/>";
//			}
//			String ip = "";
//			try {
//				InetAddress giriAddress = java.net.InetAddress.getByName(name);
//				ip = giriAddress.getHostAddress();
//			} catch (Exception e) {
//				// TODO: handle exception
//			}
//	
//			BasicDBObject doc = new BasicDBObject();
//	
//	        doc.put("whois", whois);
//	        doc.put("ip", ip);
//	        doc.put("name",name);
//	        
//	        coll.insert(doc);
//		} else {
//			System.out.println("false");
//		}
	}

}
